package com.chzn.component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Constructor;

/**
 * @Description
 * @Author houchenkai
 * @Created Date: 2020/11/9 16:29
 * @ClassName
 */
//@Component
public class UserServiceSmartInstantiationAwareBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor {

	@Override
	public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
		if (UserService.class.equals(beanClass)){
			System.out.println("推断类型");
		}
		return null;
	}

	/**
	 * TODO  之前推断构造方法的具体实现  在实例化前PostProcessors后  生产bean实例之前
	 *		自定义的determineCandidateConstructors在BeanPostProcessors列表中的顺序先于Spring中自定义的AutowiredAnnotationBeanPostProcessor
	 * @see AbstractAutowireCapableBeanFactory#createBeanInstance(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
	 * @param beanClass the raw class of the bean (never {@code null})
	 * @param beanName the name of the bean
	 * @return
	 * @throws BeansException
	 */
	@Override
	public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
               if (UserService.class.equals(beanClass)){
				   System.out.println("获取所有的");
				   Constructor<?> constructor =null;
				   try {
					   // 获取此beanClass中的单个个构造方法
					  constructor = beanClass.getConstructor();
				   } catch (NoSuchMethodException e) {
					   e.printStackTrace();
				   }
				   // 获取此beanClass中所有的构造方法
				   Constructor<?>[] constructors = beanClass.getConstructors();

				   System.out.println();
				   return new Constructor[]{constructor};
			   }
		return null;
	}

	@Override
	public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
		return null;
	}
}
